package com.hgq.jssjyyxt.util;

import com.hgq.jssjyyxt.constant.RedisConstant;
import com.hgq.jssjyyxt.exception.RRException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class ValidToken {


    public static Integer getIdByToken(HttpServletRequest request, RedisTemplate<String, Integer> redisTemplate) {

        String adminToken = request.getHeader(RedisConstant.ADMIN_TOKEN);
        String userToken = request.getHeader(RedisConstant.USER_TOKEN);
        if (StringUtils.hasLength(adminToken)) {
            Integer id = redisTemplate.opsForValue().get(adminToken);
            if (null != id && id !=0){
                return id;
            }
        }

        if (StringUtils.hasLength(userToken)) {
            Integer id = redisTemplate.opsForValue().get(userToken);
            if (null != id && id !=0){
                return id;
            }
        }
        throw new RRException("token认证失败");
    }
}
